<?php
set_time_limit(0);
$max_event = 100;

require_once('database.php');
require_once('request.php');

header('Content-Type: text/html; charset=utf-8');

function get_event_ids() {
    $result = array();
    $json_data = send_api_request('fashion.eventList.get', '{"tid":169,"country":"Singapore"}');
    $event_list = json_decode($json_data);

    $event_list = $event_list->result;
    foreach($event_list as $events) {
        foreach($events->events as $event) {
            $result[] = $event->event_id;
        }
    }

    return $result;
}

function get_event_detail($event_id) {
    $result = array();

    // Get event info
    $json_data = send_api_request('fashion.eventDetail.get', '{"country":"Singapore", "event_id":'.
            $event_id .', "order":"priceup", "page":1}');
    $event_data = json_decode($json_data)->result->data_event;

    $result['event_id'] = $event_data->nid;
    $result['name'] = $event_data->title;
    $result['start_dtm'] = $event_data->field_start_date_value;
    $result['end_dtm'] = $event_data->field_end_date_value;
    $result['thumb'] = $event_data->filepath;

    $count_product = json_decode($json_data)->result->count_product;
    $nPage = $count_product / 12;
    if ($count_product % 12) ++$nPage;

    // Get product list
    $result['product_list'] = array();
    for($page = 1; $page <= $nPage; ++$page) {
        $json_data = send_api_request('fashion.eventDetail.get', '{"country":"Singapore", "event_id":'.
                $event_id .', "order":"priceup", "page":'. $page .'}');

        $product_list = json_decode($json_data)->result->data_product;

        foreach($product_list as $product) {
            $result['product_list'][] = $product->product_id;
        }
    }
    return $result;
}

function get_product_detail($product_id) {
    $result = array();

    $json_data = send_api_request('fashion.itemdetail.get', '{"nid":'.$product_id.', "country":"Singapore"}');
    $product_data = json_decode($json_data)->result;
    $result['product_id'] = $product_data->id;
    $result['title'] = $product_data->title;
    $result['desc'] = $product_data->desc;
    $result['was_price'] = $product_data->was_price;
    $result['sell_price'] = $product_data->sell_price;
    $result['event_id'] = $product_data->event_id;
    $result['thumb'] = $product_data->images[0];

    return $result;
}

// Get Event Ids
echo 'Get event ids...<br/>'; flush();

$event_ids = get_event_ids();
sort($event_ids);
$event_ids = array_unique($event_ids);
print_r($event_ids);
echo '<br/>--------------------<br/><br/>';
flush();


// Get Event Info
echo 'Get event info...<br/>'; flush();
$event_info = array();

foreach($event_ids as $id) {
    $event_info[$id] = get_event_detail($id);

    echo $id . '<br/>'; flush();

    --$max_event;
    if ($max_event == 0) break;
}
flush();


// Get Product Ids
echo 'Get Product Ids...<br/>'; flush();
$product_ids = array();
foreach($event_info as $event) {
    $product_list = $event['product_list'];
    foreach($product_list as $product_id)
        $product_ids[] = $product_id;
}

echo 'Received ' . count($product_ids) . ' product ids<br/>'; flush();

sort($product_ids);
$product_ids = array_unique($product_ids);
print_r($product_ids);
echo '<br/>--------------------<br/><br/>';
flush();

// Get Product Info
echo 'Get product Info...<br/>'; flush();
$product_info = array();
foreach($product_ids as $product_id) {
    $product_info[$product_id] = get_product_detail($product_id);
    print_r($product_id . '<br/>'); flush();
}


// Create SQL
$db = new database('localhost', 'root', '', 'reebonz');
$db->query('TRUNCATE TABLE events');
$db->query('TRUNCATE TABLE products');
foreach($event_info as $event) {
    $db->query("INSERT INTO events(event_id, name, start_dtm, end_dtm) "
            . "VALUES (%d, %s, %s, %s)", 
            $event['event_id'], $event['name'], $event['start_dtm'], $event['end_dtm']);
}

foreach($product_info as $product) {
    $db->query("INSERT INTO products(product_id, title, `desc`, was_price, sell_price, event_id, thumb) "
            . "VALUES (%d, %s, %s, %d, %d, %d, %s)",
            $product['product_id'], $product['title'], $product['desc'], $product['was_price'],
            $product['sell_price'], $product['event_id'], $product['thumb']);
}
